//
//  OrderForm_SkuItem_TCell.swift
//  MikerShop
//
//  Created by eme on 2016/12/19.
//  Copyright © 2016年 eme. All rights reserved.
//

/*
 owner:cy
 update:2016年12月19日18:56:04
 info: 进货单cell
 VC :  OrderForm_Vc
 */
import UIKit
import Kingfisher
import RxSwift
import CommonCore
import UtilCore

class OrderForm_SkuItem_TCell: UITableViewCell {
    
    @IBOutlet weak var ischeck_Btn: UIButton!
    
    
    @IBOutlet weak var skuName1_Lb: UILabel!
    
    @IBOutlet weak var skuName2_Lb: UILabel!
    
    @IBOutlet weak var shprice_Lb: UILabel!
    
    @IBOutlet weak var cartnumber_Lb: UILabel!
    
    @IBOutlet weak var moq_Lb: UILabel!
    
    @IBOutlet weak var cartnumber_Cnv: ChangeNumView!
    
    private let disposeBag = DisposeBag()
    
    var updateBtnState_Closure:((Void)-> Void)?
    /// 更新进货单数量
    var updateCartNum_Closure:((_ param:String)-> Void)?
    var item :OrderProductModel?{
        didSet{
            if let item = self.item {
                self.skuName1_Lb.text = item.show_psfname
                self.skuName2_Lb.text = item.show_psfname2
                self.shprice_Lb.text = item.shprice.getPriceStr() + "/" + "\(item.pdunit)"
                self.moq_Lb.text = "起批量:\(item.moq)"
                self.cartnumber_Lb.text = "共:\(item.totalCartNumber)\(item.pdunit)"
                self.cartnumber_Cnv.mod = item.moq
                self.cartnumber_Cnv.cartNumber = item.cartnumber
                self.cartnumber_Cnv.stock = item.stock
                self.ischeck_Btn.isSelected = item.hasSelected
            }
        }
    }
    
    override func awakeFromNib() {
        self.ischeck_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.item!.hasSelected = !self.item!.hasSelected
                if let updateBtnState_Closure = self.updateBtnState_Closure{
                    updateBtnState_Closure()
                }
            })
            .disposed(by: disposeBag)
        self.cartnumber_Cnv
            .editnumSubject
            .asObservable()
            .subscribe(onNext: { [unowned self] ( value ) in
                self.item?.cartnumber = value
                if let item = self.item{
                    var param:[String:String] = [:]
                    param["ischeck"] = item.ischeck
                    param["cartid"] = "\(item.cartid)"
                    param["cartnumber"] = "\(value)"
                    if let updateCartNum_Closure = self.updateCartNum_Closure{
                        updateCartNum_Closure(JSONHelper.parseToJson([param] as AnyObject) as String)
                    }
                }
            })
            .disposed(by: disposeBag)
    }
}

